package top.carrotguo.weibo.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import top.carrotguo.weibo.R;

public class LoadingMoreWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private RecyclerView.Adapter mAdapter;

    public static final int TYPE_FOOTER = 1;
    public static final int TYPE_NORMAL = 2;
    public static final int LOADING_TYPE_COMPLETE = 1;
    public static final int LOADING_TYPE_LOADING = 2;
    public static final int LOADING_TYPE_END = 3;
    private int mCurrentLoading = LOADING_TYPE_COMPLETE;

    public LoadingMoreWrapper(RecyclerView.Adapter adapter){
        mAdapter = adapter;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TYPE_FOOTER) {
            View footer = LayoutInflater
                    .from(viewGroup.getContext())
                    .inflate(R.layout.footer_loading,viewGroup,false);
            return new FooterHolder(footer);
        } else {
            return mAdapter.onCreateViewHolder(viewGroup,i);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (getItemViewType(i) == TYPE_FOOTER) {
            FooterHolder holder = (FooterHolder) viewHolder;
            switch (mCurrentLoading) {
                case LOADING_TYPE_LOADING : {
                    holder.llLoading.setVisibility(View.VISIBLE);
                    holder.llEnd.setVisibility(View.GONE);
                    break;
                }
                case LOADING_TYPE_COMPLETE : {
                    holder.llLoading.setVisibility(View.INVISIBLE);
                    holder.llEnd.setVisibility(View.GONE);
                    break;
                }
                case LOADING_TYPE_END : {
                    holder.llEnd.setVisibility(View.VISIBLE);
                    holder.llLoading.setVisibility(View.GONE);
                    break;
                }
            }
        } else {
            mAdapter.onBindViewHolder(viewHolder, i);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount()-1) {
            //脚布局
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public int getItemCount() {
        return mAdapter.getItemCount()+1;
    }

    class FooterHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.ll_loading)
        LinearLayout llLoading;
        @BindView(R.id.ll_end)
        LinearLayout llEnd;

        public FooterHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public void setLoadingStates(int states){
        mCurrentLoading = states;
        //由于加载状态发现变化，所以刷新数据
        notifyDataSetChanged();
    }

}
